conkyfandomcom-20200214-history
More detailed Lua example
The following example was written to demonstrate some of what can be done with Lua and Conky. This example displays some widgets as well as an example of what can be done by combining Conky's built in variables with Lua's programming flexibility. In your conkyrc, add the following: lua_load ... TEXT ${lua_parse string_func} ${lua_bar int_func} ${lua_gauge int_func} ${lua_graph int_func} ${color}Name PID CPU% MEM% ${lua_read_parse top_cpu_colour ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}} ${lua_read_parse top_cpu_colour ${top name 2} ${top pid 2} ${top cpu 2} ${top mem 2}} ${lua_read_parse top_cpu_colour ${top name 3} ${top pid 3} ${top cpu 3} ${top mem 3}} ${lua_read_parse top_cpu_colour ${top name 4} ${top pid 4} ${top cpu 4} ${top mem 4}} ${color}Mem usage ${lua_read_parse top_mem_colour ${top_mem name 1} ${top_mem pid 1} ${top_mem cpu 1} ${top_mem mem 1}} ${lua_read_parse top_mem_colour ${top_mem name 2} ${top_mem pid 2} ${top_mem cpu 2} ${top_mem mem 2}} ${lua_read_parse top_mem_colour ${top_mem name 3} ${top_mem pid 3} ${top_mem cpu 3} ${top_mem mem 3}} Save the following Lua script somewhere to be loaded from the above conkyrc: -- Conky Lua scripting example -- -- In your conkyrc, use ${lua string_func} to call conky_string_func(), ${lua -- int_func} to call conky_int_func(), and so forth. You must load this script -- in your conkyrc using 'lua_load ' before TEXT in order to call the -- function. -- do -- configuration local interval = 5 -- local variables protected from the evil outside world local next_update local buf local int = 0 local colour = 0 local function update_buf() buf = os.time() end -- a function that returns the time with some special effects using a 5 -- second interval function conky_string_func() local now = os.time() if next_update nil or now >= next_update then update_buf(); next_update = now + interval end colour = colour + 11100 return string.format("${color #%06x}The time is now ", colour%0xffffff) .. tostring(buf) .. "${color}" end -- this function changes Conky's top colour based on a threshold function conky_top_colour(value, default_colour, upper_thresh, lower_thresh) local r, g, b = default_colour, default_colour, default_colour local colour = 0 -- in my case, there are 4 CPUs so a typical high value starts at around ~20%, and 25% is one thread/process maxed out local thresh_diff = upper_thresh - lower_thresh if (value - lower_thresh) > 0 then if value > upper_thresh then value = upper_thresh end -- add some redness, depending on the 'strength' r = math.ceil(default_colour + ((value - lower_thresh) / thresh_diff) * (0xff - default_colour)) b = math.floor(default_colour - ((value - lower_thresh) / thresh_diff) * default_colour) g = b end colour = (r * 0x10000) + (g * 0x100) + b -- no bit shifting operator in Lua afaik return string.format("${color #%06x}", colour%0xffffff) end -- parses the output from top and calls the colour function function conky_top_cpu_colour(arg) -- input is ' ${top name 1} ${top pid 1} ${top cpu 1} ${top mem 1}' local cpu = tonumber(string.match(arg, '(%d+%.%d+)')) -- tweak the last 3 parameters to your liking -- my machine has 4 CPUs, so an upper thresh of 25% is appropriate return conky_top_colour(cpu, 0xd3, 25, 15) .. arg end function conky_top_mem_colour(arg) -- input is '${top_mem name 1} ${top_mem pid 1} ${top_mem cpu 1} ${top_mem mem 1}' local mem = tonumber(string.match(arg, '%d+%.%d+%s+(%d+%.%d+)')) -- tweak the last 3 parameters to your liking -- my machine has 8GiB of ram, so an upper thresh of 15% is appropriate return conky_top_colour(mem, 0xd3, 15, 5) .. arg end -- returns a percentage value that loops around function conky_int_func() int = int + 1 return int % 100 end end Try modifying the conky_string_func() and conky_int_func() Lua code to produce different output. Category:Lua Category:Scripting